<?php
namespace app\admin\controller;

use app\admin\model\Coin as Coin_model;

class Coin extends AdminBase
{
    public function getList()
    {
        $data =  Coin_model::getInstance()->getList(10);
        $this->assign("data",$data['data']);
        $this->assign("domain",$this->domain);
        $this->assign("page",$data['page']);
        return $this->fetch();
    }

    /**
     * @return mixed|void
     */
    public function add()
    {
        if(request()->isPost()){
            $input = input();
            if(empty($input['name'])){
                $this->error('币名不可为空');
            }
            if(empty($input['full_name'])){
                $this->error('全称不可为空');
            }
            if(empty($input['main_coin_name'])){
                $this->error('主链币名不可为空');
            }
            if(empty($input['img'])){
                $this->error('logo不可为空');
            }
            if(!is_numeric($input['min_money'])){
                $this->error('最小提现数量错误');
            }
            //查询名称是否存在
            $find_data = Coin_model::getInstance()->getOne(array('name'=>strtolower($input['name'])));
            if($find_data){
                $this->error('币种已存在');
            }
            $insert_data = array(
                'name' => strtoupper($input['name']),
                'main_coin_name' => $input['main_coin_name'],
                'full_name' => $input['full_name'],
                'img' => $input['img'],
                'min_money' => $input['min_money'],
                'is_exchange' => $input['is_exchange'],
                'is_withdraw' => $input['is_withdraw'],
                'is_recharge' => $input['is_recharge'],
                'is_show' => $input['is_show'],
                'add_time' => time()
            );
            $res = Coin_model::getInstance()->insert($insert_data);
            if($res > 0){
                $this->success('添加成功','getList');
            }else{
                $this->error('添加失败，请重试');
            }
        }else{
            //加载页面
            return $this->fetch();
        }
    }

    /**
     * @return mixed
     */
    public function update()
    {
        if(request()->isPost()){
            $input = input();
            if(empty($input['main_coin_name'])){
                $this->error('主链币名不可为空');
            }
            if(empty($input['img'])){
                $this->error('logo不可为空');
            }
            if(!is_numeric($input['min_money'])){
                $this->error('最小提现数量错误');
            }
            $insert_data = array(
                'main_coin_name' => $input['main_coin_name'],
                'img' => $input['img'],
                'min_money' => $input['min_money'],
                'is_exchange' => $input['is_exchange'],
                'is_withdraw' => $input['is_withdraw'],
                'is_recharge' => $input['is_recharge'],
                'is_show' => $input['is_show'],
                'add_time' => time()
            );
            $res = Coin_model::getInstance()->where(array('id'=>$input['id']))->update($insert_data);
            if($res > 0){
                $this->success('修改成功','getList');
            }else{
                $this->error('添加失败，请重试');
            }
        }else{
            $coin_data = Coin_model::getInstance()->getOne(array('id'=>input('id')));
            if(!$coin_data){
                $this->error('币种不存在，请重试');
            }
            $this->assign('data',$coin_data);
            $this->assign('domain',$this->domain);
            //加载页面
            return $this->fetch();
        }
    }

    /**
     * 删除币种
     */
    public function del()
    {
        $id = intval(input('id'));
        $data = Coin_model::getInstance()->getOne($id);
        if(!$data){
            $this->error('币种不存在');
        }
        $res = Coin_model::getInstance()->delCoin($id);
        if($res > 0){
            $this->success('删除成功','getList');
        }else{
            $this->error('删除失败，请重试');
        }
    }
}
